export default {
  props: {
    value: Array,
    isRemote: Boolean // 是否远程【添加】或【删除】
  },
  data() {
    return {
      editable : false,
      newArray: []
    }
  },
  mounted() {
    this.init()
  },
  watch: {
    value() {
      this.init()
    },
  },
  methods: {
    init() {
      this.newArray = this.value || []
    },
    doAddPrepare() {
      this.editable = true
    },
    doClearAll() {
      this.newArray = []
    },
    doAddCancel() {
      this.editable = false
    },
    doAddConfirm(row) {
      let existed = false
      for (let r of this.newArray) {
        if (r.name === row.name) {
          existed = true
          break
        }
      }
      if (!existed) {
        if (this.isRemote) {
          this.$emit('onAdd', row)
        } else {
          this.newArray.push(row)
          this._emit()
          // this.$message.success('添加成功！')
        }
      } else {
        this.$message.error('数据重复，添加失败！')
      }
    },
    doRemove(index) {
      if (this.isRemote) {
        this.$emit('onRemove', index)
      } else {
        this.newArray.splice(index, 1);
        this._emit()
      }
    },
    _emit() {
      this.$emit('input', this.newArray)
      this.$emit('onChange', this.newArray)
    }
  }
}